package com.hy.frame.base;

import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hy.frame.common.IImageLoader;
import com.hy.frame.common.ITemplateUI;

/**
 * title BaseTFragment
 * author HeYan
 * time 2020/12/2 17:09
 * desc 无
 */
public abstract class BaseTFragment<T extends ITemplateUI> extends BaseFragment {
    private IImageLoader mImageLoader = null;
    private T mTemplateUI = null;
    private boolean mInit;

    public boolean isInit() {
        return mInit;
    }

    /**
     * 设置模板[ITemplateControl]
     */
    public abstract T buildTemplateUI();

    /**
     * 获取模板[ITemplateControl]
     */
    public T getTemplateUI() {
        return this.mTemplateUI;
    }

    /**
     * 获取图片加载器
     */
    public IImageLoader getImageLoader() {
        if (isIDestroy()) return null;
        if (this.mImageLoader == null)
            this.mImageLoader = buildImageLoader();
        return this.mImageLoader;
    }

    public abstract IImageLoader buildImageLoader();


    @Override
    public void initLayout() {
        if (this.mInit) return;
        T mTemplateUI = buildTemplateUI();
        if (mTemplateUI != null) {
            mTemplateUI.initLayout();
        }
        this.mTemplateUI = mTemplateUI;
        this.mInit = true;
    }

    @Override
    View getRootLayout(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (this.mTemplateUI == null) {
            initLayout();
        }
        if (this.mTemplateUI != null)
            return this.mTemplateUI.getRootLayout();
        return null;
    }

    @Override
    public void initView() {
        ITemplateUI mTemplateUI = getTemplateUI();
        if (mTemplateUI != null) {
            mTemplateUI.initView();
        }
    }

    @Override
    public void initData() {
        ITemplateUI mTemplateUI = getTemplateUI();
        if (mTemplateUI != null) {
            mTemplateUI.initData();
        }
    }
}
